package com.ciwong.epaper.modules.bookstore.ui;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.Subject;
import com.ciwong.epaper.modules.epaper.ui.AllServiceOrBookActivity;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 */

public class ItemSubjectList extends LinearLayout  implements View.OnClickListener{

	private RecyclerView item_recyclerview;
	private HorizontalAdapter mHorizontalAdapter;

	private  TextView tx_no_subject_tips;


	public ItemSubjectList(Context context) {
		super(context);
		LayoutInflater.from(this.getContext()).inflate(R.layout.item_subject_list, this);
		item_recyclerview = findViewById(R.id.item_recyclerview);
		tx_no_subject_tips=findViewById(R.id.tx_no_subject_tips);
		init();
	}

	protected void init() {
		item_recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
		mHorizontalAdapter = new HorizontalAdapter();
		item_recyclerview.setAdapter(mHorizontalAdapter);
	}


	public void update(List<Subject> subjectList) {


		if(null !=subjectList && subjectList.size()>0)
		{
			tx_no_subject_tips.setVisibility(GONE);
			mHorizontalAdapter.setSubjectArrayList(subjectList);
			item_recyclerview.setVisibility(VISIBLE);
		}else {
			tx_no_subject_tips.setVisibility(VISIBLE);
			item_recyclerview.setVisibility(GONE);
		}

	}

	@Override
	public void onClick(View view) {

	}


	/**
	 * 内层的RecyclerView
	 */
	private class HorizontalAdapter extends RecyclerView.Adapter<SubHolder> {

		private List<Subject> subjectArrayList = new ArrayList<>();

		private HorizontalAdapter() {

		}

		public void setSubjectArrayList(List<Subject> list) {
			subjectArrayList = list;
			notifyDataSetChanged();
		}

		@Override
		public SubHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View itemView = null;
			itemView = inflate(parent, R.layout.item_subject);
			return new SubHolder(itemView);
		}

		/**
		 * @param holder
		 * @param position
		 */
		@Override
		public void onBindViewHolder(SubHolder holder, final int position) {

			if (null != subjectArrayList && position < subjectArrayList.size()) {
				Subject subject = subjectArrayList.get(position);
				holder.tv_subject.setText(subject.getSubjectName());
				holder.mSubject=subjectArrayList.get(position);
				switch (subject.getSubjectId()) {
					case 1://	语文
						holder.img_subject.setImageResource(R.mipmap.chinese);
						break;
					case 2://	数学
						holder.img_subject.setImageResource(R.mipmap.math);
						break;
					case 3://	英语
						holder.img_subject.setImageResource(R.mipmap.english);
						break;
					case 4://	暂缺
						break;
					case 5://	思想品德
						break;
					case 6://	思想政治
						break;
					case 7://	历史
						holder.img_subject.setImageResource(R.mipmap.history);
						break;
					case 8://	地理
						holder.img_subject.setImageResource(R.mipmap.geography);
						break;
					case 9://	物理
						holder.img_subject.setImageResource(R.mipmap.physics);
						break;
					case 10://	化学
						holder.img_subject.setImageResource(R.mipmap.chemistry);
						break;
					case 11://	生物
						holder.img_subject.setImageResource(R.mipmap.biology);
						break;
					case 12://	历史与社会
						break;
					case 13://	计算机
						break;
					case 14://	文科数学
						break;
					case 15://	理科数学
						break;
					case 16://	文综合
						break;
					case 17://	理综合
						break;
					case 18://	大综合
						break;
					case 19://	品德与社会
						break;
					case 20://	体音美劳
						break;
					case 21://	信息科学与技术
						holder.img_subject.setImageResource(R.mipmap.info_tech);
						break;
					case 22://	政治
						holder.img_subject.setImageResource(R.mipmap.politics);
						break;
					case 23://	代数
						break;
					case 24://	几何
						break;
					case 25://	普及类竞赛
						break;
					case 26://	科学
						holder.img_subject.setImageResource(R.mipmap.science);
						break;
					case 27://	德育
						holder.img_subject.setImageResource(R.mipmap.moral_edu);
						break;
					case 28://	百科苑
						break;
				}
			}

		}

		@Override
		public int getItemCount() {
			if (null != subjectArrayList) {
				return subjectArrayList.size();
			} else {
				return 0;
			}
		}
	}

	class SubHolder extends RecyclerView.ViewHolder {
		TextView tv_subject;
		ImageView img_subject;
		public final View mView;
		Subject mSubject;

		public SubHolder(View itemView) {
			super(itemView);
			mView = itemView;
			tv_subject = (TextView) itemView.findViewById(R.id.tv_subject);
			img_subject = (ImageView) itemView.findViewById(R.id.img_subject);
			mView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {

					EpaperJumpManager.jumpToAllServiceOrBook(R.string.go_back, (Activity) getContext(), AllServiceOrBookActivity.TYPE_ALL_GOODS_AND_SERVICE, 0,mSubject);
				}
			});
		}
	}


	protected View inflate(ViewGroup parent, int layoutRes) {
		return LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
	}

}
